package com.pro.service.imple;

import com.pro.mapper.CourseMapper;
import com.pro.pojo.*;
import com.pro.mapper.ProductMapper;
import com.pro.service.ProductService;
import com.pro.utils.RedisCache;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: ProductServiceImple
 * @Author: Kai
 * @DATE: 2022/1/7
 * @TIME: 下午 01:57
 */
@Service
public class ProductServiceImple implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private RedisCache redisCache;

    //查询所有商品分类
    @Override
    public List<ProductCategroy> getAllProductCategroy() {
        List<ProductCategroy> allProductCategroy;
        if (redisCache.getCacheList("productTree") != null && redisCache.getCacheList("productTree").size() > 0){
            allProductCategroy = redisCache.getCacheList("productTree");
        }else{
            allProductCategroy = productMapper.getAllProductCategroy();
            redisCache.setCacheList("productTree", allProductCategroy);
        }
        return allProductCategroy;
    }

    //设置redis缓存
    public void setCache(){
        List<ProductCategroy> allProductCategroy = productMapper.getAllProductCategroy();
        redisCache.setCacheList("productTree", allProductCategroy);
    }

    //根据id删除商品分类
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteProductCategroy(Integer categoryId) throws Exception {
        if (categoryId == null || categoryId < 0){
            throw new Exception("id不正确");
        }
        Boolean delproductCategroyBoolean = productMapper.deleteProductCategroy(categoryId);
        if (delproductCategroyBoolean){
            setCache();
        }
        return delproductCategroyBoolean;
    }


    //添加一条商品分类
    @Override
    public Boolean addProductCategroy(ProductCategroy productCategroy) {
        Boolean addProductCategroy = productMapper.addProductCategroy(productCategroy);
        if (addProductCategroy){
            setCache();
        }
        return addProductCategroy;
    }

    //根据id修改一条商品分类
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateProductCategroy(ProductCategroy productCategroy) throws Exception{
        Boolean updateProductCategroyBoolean = productMapper.updateProductCategroy(productCategroy);
        if (updateProductCategroyBoolean){
            setCache();
        }
        return updateProductCategroyBoolean;
    }

    //查询所有商品
    @Override
    public List<Product> getProduct(Product product) {
        List<Product> productlist = productMapper.getProduct(product);
        return productlist;
    }

    //根据id查询商品的详情信息
    @Override
    public Product getByIdProduct(Map map) {
        Product byIdProduct = productMapper.getByIdProduct(map);
        return byIdProduct;
    }

    //根据id修改商品
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateProduct(Product product, JsCourseJournal jsCourseJournal) throws Exception {
        Boolean updateProductBoolean = productMapper.updateProduct(product);
        Boolean addCourseJournal = false;
        if (jsCourseJournal != null) {
            addCourseJournal = courseMapper.addCourseJournal(jsCourseJournal);
            if (!(addCourseJournal && updateProductBoolean)) {
                throw new Exception("修改失败");
            }
        }else {
            return updateProductBoolean;
        }
        return true;
    }


    //根据商品id查询商品状态
    @Override
    public List<CourseRecord> getCourseRecord(Integer id, Integer type) {
        List<CourseRecord> courseRecord = productMapper.getCourseRecord(id, type);
        return courseRecord;
    }

    //根据商品id删除商品
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteProduct(Integer id) throws Exception {
        if (id == null || id < 0){
            throw new Exception("id不正确");
        }
        Boolean delProductBoolean = productMapper.deleteProduct(id);
        return delProductBoolean;
    }


    //添加商品
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addProduct(Product product, CourseRecord courseRecord) throws Exception {
        Boolean productBoolean = productMapper.addProduct(product);
        courseRecord.setCourseId(product.getProductId());
        Boolean courseRecordBoolean = productMapper.addCourseRecord(courseRecord);
        if (!(productBoolean && courseRecordBoolean)){
            throw new Exception("添加失败");
        }
        return true;
    }


    //添加商品操作记录
    @Override
    public Boolean addProductOperationRecord(OperationLog operationLog) {
        Boolean addProductOperationRecordBoolean = productMapper.addProductOperationRecord(operationLog);
        return addProductOperationRecordBoolean;
    }

    /* 功能描述: <br>
     * 〈查询商品的操作记录〉
     *
     * @param: operationLog
     * @return: java.util.List<com.pro.pojo.OperationLog>
     * @Author: Kai
     * @Date: 2022/1/12 下午 04:22
     */
    @Override
    public List<OperationLog> getProductOperationLogs(OperationLog operationLog) {
        List<OperationLog> productOperationLogs = productMapper.getProductOperationLogs(operationLog);
        return productOperationLogs;
    }

    /* 功能描述: <br>
     * 〈根据id删除商品操作记录〉
     *
     * @param id
     * @return: java.lang.Boolean
     * @Author: Kai
     * @Date: 2022/1/12 下午 04:41
     */
    @Override
    public Boolean deleteProductOperationLog(Integer id) throws Exception {
        if (id == null || id < 0){
            throw new Exception("id不正确");
        }
        Boolean deleteProductOperationLogBoolean = productMapper.deleteProductOperationLog(id);
        return deleteProductOperationLogBoolean;
    }

    /* 功能描述: <br>
     * 〈根据名字查询id〉
     *
     * @param name
     * @return: java.util.List<java.lang.Integer>
     * @Author: Kai
     * @Date: 2022/1/12 下午 06:27
     */
    @Override
    public List<Integer> getByNameId(String name) {
        List<Integer> userId = productMapper.getByNameId(name);
        return userId;
    }

    /* 功能描述: <br>
     * 〈根据用户id获取商品列表〉
     *
     * @param product
     * @return: java.util.List<com.pro.pojo.Product>
     * @Author: Kai
     * @Date: 2022/1/13 下午 03:05
     */
    @Override
    public List<Product> getByUserIdProductLists(Product product) {
        List<Product> byUserIdProductLists = productMapper.getByUserIdProductLists(product);
        return byUserIdProductLists;
    }
}
